const express = require('express');
const passport = require('passport');
const path = require('path');
const app = express();
const { authenticate } = require('./authMiddleware');
const { checkExpiredLocks, checkExpiredLoans, addNextYearReservation } = require('./utils/schedule');

const loginRoutes = require('./routes/loginRoutes');
const libraryLocationRoutes = require('./routes/libraryLocationRoutes');
const userRoutes = require('./routes/userRoutes');
const journalRoutes = require('./routes/journals');
const directoryRoutes = require('./routes/journalDirectories');
const borrowRoutes = require('./routes/borrowRoutes');
const entrustmentRotes = require('./routes/entrustmentRotes');
const finesRoutes = require('./routes/finesRoutes');
const feedbackRoutes = require('./routes/feedbackRoutes');
const nextYearReservations = require('./routes/nextYearReservations');
const uploadRoutes = require('./routes/uploadRoutes.js');
const setRoutes = require('./routes/setRoutes.js');

app.use(passport.initialize());
app.use('/uploads', express.static(path.join(__dirname, 'uploads')));
app.use(express.json());


// 路由设置
app.use('/login', loginRoutes); // 登录路由
app.use('/libraryLocation', authenticate, libraryLocationRoutes);  // 馆藏地路由
app.use('/user', authenticate, userRoutes);  // 用户路由
app.use('/journals', authenticate, journalRoutes); // 期刊路由
app.use('/directories', authenticate, directoryRoutes); // 目录路由
app.use('/borrow', authenticate, borrowRoutes); // 借阅路由
app.use('/entrustments', authenticate, entrustmentRotes); // 委托路由
app.use('/fines', authenticate, finesRoutes); // 罚款路由
app.use('/feedback', authenticate, feedbackRoutes); // 反馈路由
app.use('/nextYearReservations', authenticate, nextYearReservations); // 年度征订路由
app.use('/upload', uploadRoutes); // 图片上传路由
app.use('/set', setRoutes); //常量设置路由

// 定时器任务
checkExpiredLocks();
checkExpiredLoans();
addNextYearReservation();

// 配置端口并启动服务器
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
    console.log(`服务器正在运行在端口 ${PORT}`);
});
